package org.person.gift.service.impl;

import org.person.gift.service.IBaiduAuditService;
import org.person.gift.util.AjaxResult;
import org.person.gift.util.BaiduAuditUtils;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Objects;

@Service
public class BaiduAuditServiceImpl implements IBaiduAuditService {
    @Override
    public AjaxResult audit(String auditText, String auditImgUrl) {
        AjaxResult ajaxResult = new AjaxResult();
        StringBuilder auditMsg = new StringBuilder();
        if(Objects.nonNull(auditText)){
            //审核内容不为空 才进行审核
            Map<String, Object> auditTextMap = BaiduAuditUtils.textCensor(auditText);
            //返回审核结果
            Boolean success = (Boolean)auditTextMap.get("success");
            String message = (String)auditTextMap.get("message");
            //如果审核未通过
            if(!success){
                ajaxResult.setSuccess(false);
                auditMsg.append(auditText).append("-->").append(message).append(";");
            }
            else ajaxResult.setSuccess(true);
        }
        if(Objects.nonNull(auditImgUrl)){
            Map<String, Object> auditImgMap = BaiduAuditUtils.imgCensor(auditImgUrl);
            Boolean success = (Boolean)auditImgMap.get("success");
            String message = (String)auditImgMap.get("message");
            if(!success){
                ajaxResult.setSuccess(false);
                auditMsg.append(auditImgUrl).append("-->").append(message).append(";");
            }
        }
        if(!ajaxResult.isSuccess())
            ajaxResult.setMessage(auditMsg.toString());
        return ajaxResult;
    }
}
